package top.yrlby.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import top.yrlby.dto.SetmealDto;
import top.yrlby.pojo.Dish;
import top.yrlby.pojo.Setmeal;
import top.yrlby.dao.SetmealMapper;
import top.yrlby.pojo.SetmealDish;
import top.yrlby.service.DishService;
import top.yrlby.service.SetmealDishService;
import top.yrlby.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author baiyang
 * @since 2022-09-05
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐信息
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        //获取套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历前台传递的套餐数据
        setmealDishes.stream().map(item ->{
            item.setSetmealId(String.valueOf(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }
}
